package cn.ruituo.adv6;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Hashtable;

public class Fanshe2 {
    public static void main(String[] args) throws  Exception {
        //Pig p=new Pig("猪悟能",9000,800);
        Class cc=Hashtable.class;
        Hashtable ht= (Hashtable) cc.newInstance();//一定要有无参的构造方法

        Class c=Pig.class;
        Constructor con = c.getDeclaredConstructor(String.class, int.class, int.class);
        con.setAccessible(true);//取消访问限制
        Pig p=(Pig)con.newInstance("猪悟能",9000,800);
        System.out.println(p);

        //访问私有属性
        Field fage=c.getDeclaredField("age");//获得Age属性
        fage.setAccessible(true);
        fage.setInt(p,99);
        System.out.println("修改属性后："+p);
        System.out.println("调用私有方法");
        Method mshout=c.getDeclaredMethod("shoutTo",String.class);
        mshout.setAccessible(true);
        Object rnt=mshout.invoke(p,"任小欢");
        System.out.println(rnt+"返回的就是《》》》"+rnt);
    }
}
